{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAj8AAAEGCAYAAACKMfTPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOn0lEQVR4nO3dUWiWdd8H8P/eHmgrrE0XUpOeoQdaJBGakB4keBBioBam0UFuB0ULitZBHbSTtIOgCQV6FLMCC+lAPTClkuhgW+QGgQcpNZmURHt0U2okWNzPQS/E2+vzPP7uZ1fb7t/nczi+1/W/tv+u26/X4Po11Wq1AgCQxf/M9gUAAPyVlB8AIBXlBwBIRfkBAFJRfgCAVJQfACCVv0XC7e3ttc7OzooupZTp6elQfnx8PJRvbW0N5Usp5Y477gjlm5qawmtcr/Hx8XLhwoUZWaDqvYwaGxsL5a9evRpe48477wzlb7rppvAa12sm97KUubefV65cCeVPnz4dXmPBggWh/LJly8JrXK/5dG9evHgxlI9+zjY3N4fypZRy9913h/I+Z+sTfbXNuXPnwmvMpe+3lFJGR0cv1Gq12/789VD56ezsLCMjIzN3VX8yPDwcynd3d4fyjzzySChfSil9fX2hfD03/vVavXr1jJ2r6r2M2rp1ayg/MTERXuPNN98M5Wfy5131uefafp45cyaUf+CBB8JrPPjgg6H8oUOHwmtcr/l0b7777ruh/M6dO0P5ev7xGxwcDOV9ztYn+p+Snp6e8BoDAwPhY6rU1NR0zQbnz14AQCrKDwCQivIDAKSi/AAAq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AAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x360 with 10 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "digits = load_digits()\n",
    "\n",
    "fig, axes = plt.subplots(2, 5, figsize=(10, 5),\n",
    "                         subplot_kw={'xticks':(), 'yticks': ()})\n",
    "for ax, img in zip(axes.ravel(), digits.images):\n",
    "    ax.imshow(img,cmap=plt.cm.Greys)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "image",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m     95\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 96\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     97\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'image'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-430cfcd54de8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdigits\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m     96\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     97\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 98\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     99\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    100\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setstate__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: image"
     ]
    }
   ],
   "source": [
    "digits.image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 8, 8)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1a520035f28>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAALM0lEQVR4nO3df6jV9R3H8derm2VaYr9p6maxENrYMi62EAp1G7Yi+2MwHQWLhn8VyTbCYv/snzUYRGOETbQfkKttViyi1cLsF2suf23Lroa5mndWFhGabf7I9/64R7B22/2ec76/fPd8gHTvPYfzeZ/s2ffcc7/3+3FECEAexzU9AIByETWQDFEDyRA1kAxRA8kcX8WDnuATY7wmVvHQnymHJ9f373D6tLdrW+utg5NqW+vA1sO1rVWn/2ifDsR+j3ZbJVGP10Rd7HlVPPRnyodzL65trZV33F7bWre9Ob+2tXZ9bW9ta9VpXaz51Nt4+Q0kQ9RAMkQNJEPUQDJEDSRD1EAyRA0kQ9RAMkQNJFMoatvzbW+zvd320qqHAtC7MaO2PSDpTkmXS7pA0iLbF1Q9GIDeFDlSz5K0PSJ2RMQBSQ9KWlDtWAB6VSTqKZJ2HvX5cOdrH2N7se31ttcf1P6y5gPQpSJRj/brXf9ztcKIWB4RgxExOE4n9j8ZgJ4UiXpY0rSjPp8qaVc14wDoV5GoX5J0vu1zbZ8gaaGkR6sdC0CvxrxIQkQcsn2DpCclDUi6OyK2VD4ZgJ4UuvJJRDwu6fGKZwFQAs4oA5IhaiAZogaSIWogGaIGkiFqIBmiBpKpZIeOrA5fNrPW9Z6/81e1rfXqwdqW0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TbA5L+JemRascC0KtuX37Pk/RaRLxRxTAA+tftL3QslPTAaDfYXixpsSSNZ/88oDGFj9Sda35fJel3o93OtjtAO3Tz8vtySRsj4u2qhgHQv26iXqRPeekNoD0KRW17gqRvSHq42nEA9KvotjsfSjq94lkAlIAzyoBkiBpIhqiBZIgaSIaogWSIGkiGqIFkiBpIxhFR/oPa70jq9tczz5D0bunDtEPW58bzas4XIuLM0W6oJOpe2F4fEYNNz1GFrM+N59VOvPwGkiFqIJk2Rb286QEqlPW58bxaqDXfUwMoR5uO1ABKQNRAMq2I2vZ829tsb7e9tOl5ymB7mu21todsb7F9U9Mzlcn2gO1Nth9repYy2Z5se7XtrZ2/u0uanqlbjX9P3dkg4FWNXC5pWNJLkhZFxCuNDtYn2+dIOiciNto+RdIGSVcf68/rCNs/kDQoaVJEXNn0PGWxfZ+k5yNiRecKuhMi4v2Gx+pKG47UsyRtj4gdEXFA0oOSFjQ8U98i4s2I2Nj5eK+kIUlTmp2qHLanSrpC0oqmZymT7UmSLpW0UpIi4sCxFrTUjqinSNp51OfDSvIf/xG2p0uaKWldw6OU5Q5JN0s63PAcZTtP0juS7ul8a7HC9sSmh+pWG6L2KF9L83M22ydLekjSkojY0/Q8/bJ9paTdEbGh6VkqcLykiyQti4iZkvZJOube42lD1MOSph31+VRJuxqapVS2x2kk6FURkeXyyrMlXWX7dY18qzTX9v3NjlSaYUnDEXHkFdVqjUR+TGlD1C9JOt/2uZ03JhZKerThmfpm2xr53mwoIm5vep6yRMQtETE1IqZr5O/q6Yi4puGxShERb0naaXtG50vzJB1zb2x2u0Fe6SLikO0bJD0paUDS3RGxpeGxyjBb0rWS/m57c+drt0bE482NhAJulLSqc4DZIem6hufpWuM/0gJQrja8/AZQIqIGkiFqIBmiBpIhaiAZogaSIWogmf8CC0CmOH9yAqMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "gi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.target.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=[]\n",
    "for i in range(10):\n",
    "    for j in range(1,501):\n",
    "        digit = cv2.imread('./data/%d/%d_%d.bmp' % ( i,i,j))\n",
    "        X.append(digit[::,0])\n",
    "        \n",
    "X = np.asarray(X) # numpy 对象\n",
    "y= np.array([i for i in range(10)] * 500)\n",
    "y.sort()\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0.,  0.,  5., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 13., ..., 15.,  5.,  0.],\n",
       "        [ 0.,  3., 15., ..., 11.,  8.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  4., 11., ..., 12.,  7.,  0.],\n",
       "        [ 0.,  2., 14., ..., 12.,  0.,  0.],\n",
       "        [ 0.,  0.,  6., ...,  0.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  0., ...,  5.,  0.,  0.],\n",
       "        [ 0.,  0.,  0., ...,  9.,  0.,  0.],\n",
       "        [ 0.,  0.,  3., ...,  6.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0.,  1., ...,  6.,  0.,  0.],\n",
       "        [ 0.,  0.,  1., ...,  6.,  0.,  0.],\n",
       "        [ 0.,  0.,  0., ..., 10.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  0., ..., 12.,  0.,  0.],\n",
       "        [ 0.,  0.,  3., ..., 14.,  0.,  0.],\n",
       "        [ 0.,  0.,  8., ..., 16.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  9., 16., ...,  0.,  0.,  0.],\n",
       "        [ 0.,  3., 13., ..., 11.,  5.,  0.],\n",
       "        [ 0.,  0.,  0., ..., 16.,  9.,  0.]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 0.,  0.,  1., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 13., ...,  2.,  1.,  0.],\n",
       "        [ 0.,  0., 16., ..., 16.,  5.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0., 16., ..., 15.,  0.,  0.],\n",
       "        [ 0.,  0., 15., ..., 16.,  0.,  0.],\n",
       "        [ 0.,  0.,  2., ...,  6.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  2., ...,  0.,  0.,  0.],\n",
       "        [ 0.,  0., 14., ..., 15.,  1.,  0.],\n",
       "        [ 0.,  4., 16., ..., 16.,  7.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0.,  0., ..., 16.,  2.,  0.],\n",
       "        [ 0.,  0.,  4., ..., 16.,  2.,  0.],\n",
       "        [ 0.,  0.,  5., ..., 12.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0., 10., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  2., 16., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 15., ..., 15.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  4., 16., ..., 16.,  6.,  0.],\n",
       "        [ 0.,  8., 16., ..., 16.,  8.,  0.],\n",
       "        [ 0.,  1.,  8., ..., 12.,  1.,  0.]]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "digits = load_digits()\n",
    "X= digits.images\n",
    "y=digits.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 8, 8)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=X.reshape(X.shape[0],-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1347,)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(n_neighbors=63)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=63)\n",
    "knn.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 8, 3, 0, 1, 7, 6, 6, 8, 5, 5, 4, 4, 1, 8, 0, 0, 0, 9, 5, 6,\n",
       "       4, 0, 8, 3, 6, 4, 7, 4, 1, 8, 2, 9, 2, 4, 8, 3, 3, 1, 1, 2, 8, 5,\n",
       "       6, 2, 2, 6, 2, 8, 3, 1, 7, 7, 6, 2, 1, 1, 3, 2, 1, 9, 3, 8, 5, 0,\n",
       "       0, 7, 3, 7, 2, 7, 7, 4, 2, 0, 9, 5, 9, 2, 9, 6, 1, 5, 3, 2, 2, 3,\n",
       "       3, 1, 0, 6, 4, 8, 6, 3, 7, 5, 2, 7, 9, 8, 4, 5, 6, 1, 6, 0, 9, 6,\n",
       "       2, 1, 6, 2, 0, 6, 2, 4, 8, 5, 9, 6, 7, 8, 9, 2, 6, 4, 2, 3, 3, 6,\n",
       "       6, 9, 1, 8, 4, 6, 0, 0, 7, 9, 3, 8, 9, 6, 4, 1, 5, 6, 4, 5, 6, 1,\n",
       "       9, 8, 9, 2, 4, 5, 1, 9, 8, 4, 5, 8, 6, 4, 2, 1, 0, 1, 5, 8, 5, 7,\n",
       "       1, 3, 6, 4, 3, 5, 8, 5, 1, 1, 8, 1, 5, 7, 0, 8, 8, 4, 3, 7, 0, 3,\n",
       "       3, 8, 8, 2, 3, 0, 1, 1, 0, 6, 7, 2, 6, 4, 2, 0, 6, 9, 8, 6, 3, 4,\n",
       "       7, 8, 0, 6, 8, 5, 0, 0, 9, 4, 7, 8, 7, 5, 8, 0, 0, 1, 7, 5, 2, 3,\n",
       "       5, 7, 6, 6, 2, 4, 6, 6, 9, 2, 1, 5, 2, 4, 7, 1, 5, 3, 3, 2, 0, 3,\n",
       "       3, 1, 2, 0, 3, 3, 2, 1, 0, 8, 4, 7, 3, 6, 4, 1, 0, 8, 0, 8, 8, 8,\n",
       "       4, 1, 7, 4, 7, 3, 3, 8, 3, 5, 7, 3, 0, 0, 3, 7, 2, 4, 7, 1, 9, 5,\n",
       "       3, 0, 4, 3, 4, 3, 3, 0, 6, 1, 8, 4, 5, 3, 7, 7, 1, 0, 9, 7, 2, 9,\n",
       "       2, 1, 6, 4, 6, 4, 0, 7, 7, 2, 5, 3, 7, 5, 5, 3, 7, 3, 1, 7, 0, 4,\n",
       "       7, 7, 9, 2, 3, 8, 7, 0, 9, 2, 0, 0, 6, 6, 5, 7, 1, 6, 5, 7, 7, 9,\n",
       "       4, 9, 9, 3, 0, 1, 9, 3, 4, 3, 6, 5, 5, 4, 6, 5, 7, 4, 9, 3, 4, 5,\n",
       "       5, 8, 7, 3, 2, 8, 4, 1, 2, 0, 5, 7, 6, 5, 9, 7, 8, 9, 2, 6, 3, 8,\n",
       "       5, 3, 0, 0, 9, 3, 8, 3, 7, 8, 4, 5, 2, 9, 7, 7, 7, 0, 3, 6, 5, 4,\n",
       "       3, 2, 2, 5, 5, 7, 5, 7, 2, 4])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9288888888888889"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(knn.predict(X_test) == y_test).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9288888888888889"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn.score(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1a523700b00>"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKxUlEQVR4nO3d34tc9RnH8c+nq9LGHyy0tkg2NAoSkEI3EgISkDS2JVbRXvQiAYWVQq4UQwuivbL/gG4virBE3YCp0kYFEasVdLFCa03itjXZpKRhS7bRRimLPwoNiU8vdgLRrt0zM+d8z9mn7xcs7s4O+30mydszMztzvo4IAcjjC20PAKBeRA0kQ9RAMkQNJEPUQDIXNfFDbad8Sn1kZKToeuvWrSu21po1a4qtVdKxY8eKrnfu3Llia0WEl7vcTfxKK2vUo6OjRdebnJwsttb4+HixtUraunVr0fUWFxeLrfV5UXP3G0iGqIFkiBpIhqiBZIgaSIaogWSIGkiGqIFkiBpIplLUtrfbPmb7uO37mx4KwOBWjNr2iKSfS7pZ0nWSdtq+runBAAymypF6s6TjEXEiIs5IekrS7c2OBWBQVaJeK+nkBV8v9C77FNu7bB+wfaCu4QD0r8pbL5d7J8h/vQsrIqYkTUl536UFrAZVjtQLki58Y++YpFPNjANgWFWiflPStbavtn2JpB2Snmt2LACDWvHud0SctX23pJckjUh6LCIONz4ZgIFUOp1RRLwg6YWGZwFQA15RBiRD1EAyRA0kQ9RAMkQNJEPUQDJEDSTTyLY7JZXcNWNmZqbYWpI0OztbbK3du3cXW2t6errYWqV3Hin9b2Q5HKmBZIgaSIaogWSIGkiGqIFkiBpIhqiBZIgaSIaogWSIGkimyg4dj9k+bfvtEgMBGE6VI/W0pO0NzwGgJitGHRGvSfpngVkA1KC2d2nZ3iVpV10/D8BgaouabXeAbuDZbyAZogaSqfIrrScl/U7SBtsLtn/Y/FgABlVlL62dJQ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ZBMJ6K2vd32MdvHbd/f9jx1sL3O9qu252wftn1v2zPVyfaI7bdsP9/2LHWyPWp7v+2jvb+7G9qeqV+tP6bubRDwFy2dLmlB0puSdkbEkVYHG5LtqyRdFRGHbF8u6aCk76/223We7R9J2iTpioi4te156mJ7r6TfRsSe3hl010TEYstj9aULR+rNko5HxImIOCPpKUm3tzzT0CLinYg41Pv8Q0lzkta2O1U9bI9JukXSnrZnqZPtKyTdKOlRSYqIM6staKkbUa+VdPKCrxeU5B//ebbXS9oo6Y2WR6nLpKT7JH3S8hx1u0bSe5Ie7z202GP70raH6lcXovYyl6X5PZvtyyQ9LWl3RHzQ9jzDsn2rpNMRcbDtWRpwkaTrJT0SERslfSxp1T3H04WoFyStu+DrMUmnWpqlVrYv1lLQ+yIiy+mVt0i6zfa8lh4qbbP9RLsj1WZB0kJEnL9HtV9Lka8qXYj6TUnX2r6698TEDknPtTzT0GxbS4/N5iLiobbnqUtEPBARYxGxXkt/V69ExB0tj1WLiHhX0knbG3oX3SRp1T2x2e8GebWLiLO275b0kqQRSY9FxOGWx6rDFkl3Svqz7dneZT+JiBfaGwkV3CNpX+8Ac0LSXS3P07fWf6UFoF5duPsNoEZEDSRD1EAyRA0kQ9RAMkQNJEPUQDL/AUqNjvbnovjkAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "index= np.random.randint(0,1200,size=1)[0]\n",
    "plt.imshow(digits.images[index],cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 8, 8)"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmpX = digits.images\n",
    "tmpX.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1797):\n",
    "    for y in range(8):\n",
    "        for x in range(8):\n",
    "            if tmpX[i][y,x] < 10:\n",
    "                tmpX[i][y,x]=0\n",
    "            else:\n",
    "                 tmpX[i][y,x]=16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1a523bbcd30>"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKdklEQVR4nO3dX4hc5RnH8d+vG0MbNeSif0iTUA1ILtqLxoQUSSmt1pJiUC96kUB7IYW9MiSUIlboRS96W/SqENbYgKlSooIE0Qq11UJNk00iJtkoabBkG20Ua6O9CWmeXuwJRLo7c2bmPXPOPvv9QMjO7Jnlmez88r4z5z3P64gQgDw+03YBAMoi1EAyhBpIhlADyRBqIJllTfxQ23ykXsCmTZvaLmHRm56ebruExkSE57vfTZzSItRlcLpxdPa8r/sUFgo1028gGUINJEOogWQINZAMoQaSIdRAMoQaSIZQA8kQaiCZWqG2vc32W7bP2n646aIADK/vMlHbE5LelnS3pFlJRyTtjIjTPR7D+sYCWCY6OpaJzm+LpLMRcS4iLkt6WtJ9JYsDUE6dUK+RdP6627PVfZ9ie9L2UdtHSxUHYHB1Lr2cb4j/v3lhROyVtFdi+g20qc5IPStp3XW310q60Ew5AEZVJ9RHJN1m+1bbyyXtkPR8s2UBGFbf6XdEXLH9oKSXJE1I2hcRpxqvDMBQ6HzSYZzSGh2ntAAseoQaSIZQA8kQaiAZQg0kQ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Hq0S3FULOiDEiGUAPJEGogGUINJEOogWQINZAMoQaSIdRAMk1tu/OBpEEvz/x89biMhnpui2DhRNbf2WJ4Xl9Z6BuNrCgbhu2jEbG57TqakPW58by6iek3kAyhBpLpUqj3tl1Ag7I+N55XB3XmPTWAMro0UgMogFADyXQi1La32X7L9lnbD7ddTwm219l+xfaM7VO2d7ddU0m2J2wft32o7VpKsr3K9kHbZ6rf3R1t1zSo1t9TVxsEvC3pbkmzko5I2hkRp1stbES2V0taHRHHbN8saVrS/Yv9eV1j+yeSNktaGRHb266nFNv7Jb0WEVNVB90VEfFRy2UNpAsj9RZJZyPiXERclvS0pPtarmlkEfFuRByrvv5Y0oykNe1WVYbttZLukTTVdi0l2V4p6VuSHpekiLi82AItdSPUazTX1PCaWSV58V9j+xZJGyUdbrmUUh6V9JCkqy3XUdp6Se9LeqJ6azFl+8a2ixpUF0I93wLnNOfZbN8k6RlJeyLiUtv1jMr2dkkXI2K67VoasEzS7ZJ+HREbJf1H0qL7jKcLoZ6VtO6622s1txPIomf7Bs0F+kBEPNt2PYVslXSv7Xc091bpTttPtltSMbOSZiPi2ozqoOZCvqh0IdRHJN1m+9bqg4kdkp5vuaaRee4Sq8clzUTEr9qup5SI+FlErI2IWzT3u/pDRPyw5bKKiIj3JJ23vaG66y5Ji+6DzaYuvawtIq7YflDSS5ImJO2LiFMtl1XCVkk/kvSm7RPVfY9ExAvtlYQadkk6UA0w5yQ90HI9A2v9lBaAsrow/QZQEKEGkiHUQDKEGkiGUAPJEGogGUINJPM/iWEZTcpJvFsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(tmpX[index],cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  0.,  4., 15., 12.,  0.,  0.],\n",
       "       [ 0.,  0.,  3., 16., 15., 14.,  0.,  0.],\n",
       "       [ 0.,  0.,  8., 13.,  8., 16.,  0.,  0.],\n",
       "       [ 0.,  0.,  1.,  6., 15., 11.,  0.,  0.],\n",
       "       [ 0.,  1.,  8., 13., 15.,  1.,  0.,  0.],\n",
       "       [ 0.,  9., 16., 16.,  5.,  0.,  0.,  0.],\n",
       "       [ 0.,  3., 13., 16., 16., 11.,  5.,  0.],\n",
       "       [ 0.,  0.,  0.,  3., 11., 16.,  9.,  0.]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmpX[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = np.asarray(tmpX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13., 15., 15., ..., 12., 14., 13.],\n",
       "       [13., 16., 16., ..., 16., 16., 16.],\n",
       "       [15., 16., 16., ..., 16., 16., 16.],\n",
       "       ...,\n",
       "       [15., 16., 16., ..., 16., 16., 13.],\n",
       "       [10., 16., 16., ..., 16., 16., 16.],\n",
       "       [14., 16., 15., ..., 16., 16., 14.]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(aa,axis=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
